package dbexp.connection.model;

import dbexp.model.db.DbUser;
import dbexp.utils.DbProperties;

/**
 * @author Michał Skrzyński
 * @email michal_skrzynski@o2.pl
 */
public class Client implements ConnectionInfo {
	private String name = null;
	private String databaseURL = null;
	private String username = null;
	private String password = null;
	private DbUser u = new DbUser();

	/**
	 * Konstruktor
	 */
	public Client(DbUser u) {
		this.name = u.getConnectionName();
		this.databaseURL = DbProperties.getInstance().getURL(u);
		this.username = u.getSchemaName();
		this.password = u.getPass();
		this.u = u;
	}

	public String getName() {
		return this.name;
	}
	public String getDatabaseURL() {
		return this.databaseURL;
	}
	public String getUsername() {
		return this.username.toUpperCase();
	}
	public String getPassword() {
		return this.password;
	}

	public int hashCode() {
		return this.databaseURL.hashCode()
				+ this.username.hashCode() + this.password.hashCode();
	}

	public boolean equals(Object obj) {
		boolean equal = false;
		if (obj instanceof Client) {
			Client otherClientInfo = (Client) obj;
			String otherClientInfoName = otherClientInfo.getName();
			if (this.getName().equals(otherClientInfoName)) {
				equal = true;
			}
		}
		return equal;
	}

	public DbUser getUser() {
		return this.u;
	}

	/**
	 * For debugging only
	 * @return toString
	 */ 
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("Connection Info\n");
		buffer.append("Name: " + this.getName() + "\n");
		buffer.append("DBMS Type: " + DbProperties.getInstance().getDbType() + "\n");
		buffer.append("Driver name: " + DbProperties.getInstance().getDrivertName() + "\n");
		buffer.append("Database URL: " + this.getDatabaseURL() + "\n");
		buffer.append("Username: " + this.getUsername() + "\n");
		buffer.append("Password: " + this.getPassword() + "\n");

		return buffer.toString();
	}

	@Override
	public String getDriverName() {
		return "oracle.jdbc.driver.OracleDriver";
	}
}
